#Boa:Dialog:Dialog1

import wx
import wx.grid
import utiles
import consultasBD
from decimal import Decimal
import os

ROJO = (255, 145, 145, 255)
VERDE = (145, 255, 145, 255)
AZUL = (145, 145, 255, 255)
BLANCO = (255, 255, 255, 255)
AMARILLO = (254, 248, 191, 255)

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1BUTTON1, wxID_DIALOG1BUTTON2, wxID_DIALOG1BUTTON3, 
 wxID_DIALOG1BUTTON4, wxID_DIALOG1BUTTON5, wxID_DIALOG1BUTTON6, 
 wxID_DIALOG1BUTTON7, wxID_DIALOG1BUTTON8, wxID_DIALOG1COMBOBOX1, 
 wxID_DIALOG1COMBOBOX10, wxID_DIALOG1COMBOBOX2, wxID_DIALOG1COMBOBOX3, 
 wxID_DIALOG1COMBOBOX4, wxID_DIALOG1COMBOBOX5, wxID_DIALOG1COMBOBOX6, 
 wxID_DIALOG1COMBOBOX7, wxID_DIALOG1COMBOBOX8, wxID_DIALOG1COMBOBOX9, 
 wxID_DIALOG1GRID1, wxID_DIALOG1STATICTEXT1, wxID_DIALOG1STATICTEXT10, 
 wxID_DIALOG1STATICTEXT11, wxID_DIALOG1STATICTEXT12, wxID_DIALOG1STATICTEXT13, 
 wxID_DIALOG1STATICTEXT14, wxID_DIALOG1STATICTEXT15, wxID_DIALOG1STATICTEXT16, 
 wxID_DIALOG1STATICTEXT17, wxID_DIALOG1STATICTEXT18, wxID_DIALOG1STATICTEXT19, 
 wxID_DIALOG1STATICTEXT2, wxID_DIALOG1STATICTEXT20, wxID_DIALOG1STATICTEXT21, 
 wxID_DIALOG1STATICTEXT23, wxID_DIALOG1STATICTEXT24, wxID_DIALOG1STATICTEXT25, 
 wxID_DIALOG1STATICTEXT26, wxID_DIALOG1STATICTEXT27, wxID_DIALOG1STATICTEXT28, 
 wxID_DIALOG1STATICTEXT29, wxID_DIALOG1STATICTEXT3, wxID_DIALOG1STATICTEXT30, 
 wxID_DIALOG1STATICTEXT4, wxID_DIALOG1STATICTEXT5, wxID_DIALOG1STATICTEXT6, 
 wxID_DIALOG1STATICTEXT7, wxID_DIALOG1STATICTEXT8, wxID_DIALOG1STATICTEXT9, 
 wxID_DIALOG1TEXTCTRL1, wxID_DIALOG1TEXTCTRL10, wxID_DIALOG1TEXTCTRL11, 
 wxID_DIALOG1TEXTCTRL12, wxID_DIALOG1TEXTCTRL13, wxID_DIALOG1TEXTCTRL14, 
 wxID_DIALOG1TEXTCTRL15, wxID_DIALOG1TEXTCTRL16, wxID_DIALOG1TEXTCTRL17, 
 wxID_DIALOG1TEXTCTRL18, wxID_DIALOG1TEXTCTRL19, wxID_DIALOG1TEXTCTRL2, 
 wxID_DIALOG1TEXTCTRL20, wxID_DIALOG1TEXTCTRL21, wxID_DIALOG1TEXTCTRL22, 
 wxID_DIALOG1TEXTCTRL23, wxID_DIALOG1TEXTCTRL24, wxID_DIALOG1TEXTCTRL25, 
 wxID_DIALOG1TEXTCTRL26, wxID_DIALOG1TEXTCTRL27, wxID_DIALOG1TEXTCTRL28, 
 wxID_DIALOG1TEXTCTRL29, wxID_DIALOG1TEXTCTRL3, wxID_DIALOG1TEXTCTRL30, 
 wxID_DIALOG1TEXTCTRL31, wxID_DIALOG1TEXTCTRL32, wxID_DIALOG1TEXTCTRL33, 
 wxID_DIALOG1TEXTCTRL34, wxID_DIALOG1TEXTCTRL35, wxID_DIALOG1TEXTCTRL36, 
 wxID_DIALOG1TEXTCTRL37, wxID_DIALOG1TEXTCTRL38, wxID_DIALOG1TEXTCTRL39, 
 wxID_DIALOG1TEXTCTRL4, wxID_DIALOG1TEXTCTRL5, wxID_DIALOG1TEXTCTRL6, 
 wxID_DIALOG1TEXTCTRL7, wxID_DIALOG1TEXTCTRL8, wxID_DIALOG1TEXTCTRL9, 
] = [wx.NewId() for _init_ctrls in range(88)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(27, 5), size=wx.Size(810, 602),
              style=wx.DEFAULT_DIALOG_STYLE, title='Orden de Compra')
        self.SetClientSize(wx.Size(802, 575))

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label='Cliente', name='staticText1', parent=self, pos=wx.Point(8,
              40), size=wx.Size(33, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_DIALOG1STATICTEXT2,
              label='Direccion', name='staticText2', parent=self,
              pos=wx.Point(288, 40), size=wx.Size(43, 13), style=0)

        self.staticText3 = wx.StaticText(id=wxID_DIALOG1STATICTEXT3,
              label='Telefono', name='staticText3', parent=self,
              pos=wx.Point(544, 40), size=wx.Size(42, 13), style=0)

        self.staticText4 = wx.StaticText(id=wxID_DIALOG1STATICTEXT4,
              label='Tarjeta Sussan', name='staticText4', parent=self,
              pos=wx.Point(88, 40), size=wx.Size(72, 13), style=0)

        self.staticText5 = wx.StaticText(id=wxID_DIALOG1STATICTEXT5,
              label='Situacion Fiscal', name='staticText5', parent=self,
              pos=wx.Point(288, 80), size=wx.Size(72, 13), style=0)

        self.staticText6 = wx.StaticText(id=wxID_DIALOG1STATICTEXT6,
              label='e-mail', name='staticText6', parent=self, pos=wx.Point(544,
              80), size=wx.Size(28, 13), style=0)

        self.staticText7 = wx.StaticText(id=wxID_DIALOG1STATICTEXT7,
              label='Cumple', name='staticText7', parent=self, pos=wx.Point(720,
              40), size=wx.Size(35, 13), style=0)

        self.staticText8 = wx.StaticText(id=wxID_DIALOG1STATICTEXT8,
              label='Fecha', name='staticText8', parent=self, pos=wx.Point(8,
              124), size=wx.Size(29, 13), style=0)

        self.staticText9 = wx.StaticText(id=wxID_DIALOG1STATICTEXT9,
              label='Sucursal', name='staticText9', parent=self,
              pos=wx.Point(531, 0), size=wx.Size(40, 13), style=0)

        self.staticText10 = wx.StaticText(id=wxID_DIALOG1STATICTEXT10,
              label='Vendedora', name='staticText10', parent=self,
              pos=wx.Point(664, 0), size=wx.Size(53, 13), style=0)

        self.staticText17 = wx.StaticText(id=wxID_DIALOG1STATICTEXT17,
              label='Buscar por id,nombre,direccion,telefono,nro.doc o email',
              name='staticText17', parent=self, pos=wx.Point(8, 0),
              size=wx.Size(269, 13), style=0)

        self.textCtrl15 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL15,
              name='textCtrl15', parent=self, pos=wx.Point(8, 16),
              size=wx.Size(264, 21), style=0, value='')
        self.textCtrl15.Bind(wx.EVT_KILL_FOCUS, self.OnKillBuscarCliente)

        self.button2 = wx.Button(id=wxID_DIALOG1BUTTON2, label='Buscar Cliente',
              name='button2', parent=self, pos=wx.Point(288, 16),
              size=wx.Size(80, 23), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnBuscarCliente,
              id=wxID_DIALOG1BUTTON2)

        self.button4 = wx.Button(id=wxID_DIALOG1BUTTON4, label='Alta Cliente',
              name='button4', parent=self, pos=wx.Point(368, 16),
              size=wx.Size(80, 23), style=0)
        self.button4.Bind(wx.EVT_BUTTON, self.OnNuevoCliente,
              id=wxID_DIALOG1BUTTON4)

        self.button3 = wx.Button(id=wxID_DIALOG1BUTTON3, label='Modificar',
              name='button3', parent=self, pos=wx.Point(448, 16),
              size=wx.Size(80, 23), style=0)
        self.button3.Bind(wx.EVT_BUTTON, self.OnModifCliente,
              id=wxID_DIALOG1BUTTON3)

        self.textCtrl1 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL1, name='textCtrl1',
              parent=self, pos=wx.Point(8, 56), size=wx.Size(72, 21), style=0,
              value='')
        self.textCtrl1.SetEditable(False)
        self.textCtrl1.Bind(wx.EVT_KILL_FOCUS, self.OnKillCliente1)

        self.textCtrl2 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL2, name='textCtrl2',
              parent=self, pos=wx.Point(8, 96), size=wx.Size(264, 21), style=0,
              value='')
        self.textCtrl2.SetEditable(False)
        self.textCtrl2.Bind(wx.EVT_KILL_FOCUS, self.OnKillCliente2)

        self.textCtrl14 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL14,
              name='textCtrl14', parent=self, pos=wx.Point(88, 56),
              size=wx.Size(184, 21), style=0, value='')
        self.textCtrl14.SetEditable(False)

        self.comboBox1 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX1,
              name='comboBox1', parent=self, pos=wx.Point(531, 16),
              size=wx.Size(130, 21), style=0, value='')
        self.comboBox1.SetLabel('')
        self.comboBox1.Bind(wx.EVT_COMBOBOX, self.OnCambiaComboSucursal,
              id=wxID_DIALOG1COMBOBOX1)

        self.comboBox2 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX2,
              name='comboBox2', parent=self, pos=wx.Point(664, 16),
              size=wx.Size(130, 21), style=0, value='')
        self.comboBox2.SetLabel('')
        self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCambiaVendedora,
              id=wxID_DIALOG1COMBOBOX2)

        self.textCtrl3 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL3, name='textCtrl3',
              parent=self, pos=wx.Point(288, 56), size=wx.Size(256, 21),
              style=0, value='')
        self.textCtrl3.SetEditable(False)

        self.textCtrl4 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL4, name='textCtrl4',
              parent=self, pos=wx.Point(544, 56), size=wx.Size(176, 21),
              style=0, value='')
        self.textCtrl4.SetEditable(False)

        self.textCtrl5 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL5, name='textCtrl5',
              parent=self, pos=wx.Point(288, 96), size=wx.Size(112, 21),
              style=0, value='')
        self.textCtrl5.SetEditable(False)

        self.textCtrl6 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL6, name='textCtrl6',
              parent=self, pos=wx.Point(544, 96), size=wx.Size(176, 21),
              style=0, value='')
        self.textCtrl6.SetEditable(False)

        self.textCtrl7 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL7, name='textCtrl7',
              parent=self, pos=wx.Point(720, 56), size=wx.Size(40, 21), style=0,
              value='')
        self.textCtrl7.SetEditable(False)

        self.textCtrl8 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL8, name='textCtrl8',
              parent=self, pos=wx.Point(48, 124), size=wx.Size(72, 21), style=0,
              value='')
        self.textCtrl8.SetEditable(False)

        self.staticText11 = wx.StaticText(id=wxID_DIALOG1STATICTEXT11,
              label='Documento', name='staticText11', parent=self,
              pos=wx.Point(400, 80), size=wx.Size(54, 13), style=0)

        self.staticText12 = wx.StaticText(id=wxID_DIALOG1STATICTEXT12,
              label='Observaciones', name='staticText12', parent=self,
              pos=wx.Point(288, 128), size=wx.Size(71, 13), style=0)

        self.staticText13 = wx.StaticText(id=wxID_DIALOG1STATICTEXT13,
              label='Subtotal c/desc.', name='staticText13', parent=self,
              pos=wx.Point(472, 408), size=wx.Size(78, 13), style=0)

        self.staticText14 = wx.StaticText(id=wxID_DIALOG1STATICTEXT14,
              label='IVA', name='staticText14', parent=self, pos=wx.Point(616,
              408), size=wx.Size(17, 13), style=0)

        self.staticText15 = wx.StaticText(id=wxID_DIALOG1STATICTEXT15,
              label='TOTAL', name='staticText15', parent=self, pos=wx.Point(700,
              408), size=wx.Size(32, 13), style=0)

        self.staticText16 = wx.StaticText(id=wxID_DIALOG1STATICTEXT16,
              label='Nombre', name='staticText16', parent=self, pos=wx.Point(8,
              80), size=wx.Size(37, 13), style=0)

        self.textCtrl9 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL9, name='textCtrl9',
              parent=self, pos=wx.Point(400, 96), size=wx.Size(144, 21),
              style=0, value='')
        self.textCtrl9.SetEditable(False)

        self.textCtrl11 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL11,
              name='textCtrl11', parent=self, pos=wx.Point(552, 408),
              size=wx.Size(56, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl11.SetEditable(False)

        self.textCtrl12 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL12,
              name='textCtrl12', parent=self, pos=wx.Point(640, 408),
              size=wx.Size(56, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl12.SetEditable(False)

        self.textCtrl13 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL13,
              name='textCtrl13', parent=self, pos=wx.Point(736, 408),
              size=wx.Size(56, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl13.SetEditable(False)

        self.textCtrl10 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL10,
              name='textCtrl10', parent=self, pos=wx.Point(368, 128),
              size=wx.Size(352, 21), style=0, value='')
        self.textCtrl10.SetMaxLength(55)
        self.textCtrl10.Bind(wx.EVT_KILL_FOCUS, self.OnKillObservacion)

        self.button1 = wx.Button(id=wxID_DIALOG1BUTTON1, label='Limpiar',
              name='button1', parent=self, pos=wx.Point(520, 548),
              size=wx.Size(64, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnLimpiar, id=wxID_DIALOG1BUTTON1)

        self.button6 = wx.Button(id=wxID_DIALOG1BUTTON6, label='Salir',
              name='button6', parent=self, pos=wx.Point(728, 128),
              size=wx.Size(64, 23), style=0)
        self.button6.Bind(wx.EVT_BUTTON, self.OnSalir, id=wxID_DIALOG1BUTTON6)

        self.button7 = wx.Button(id=wxID_DIALOG1BUTTON7, label='Crear Senia',
              name='button7', parent=self, pos=wx.Point(8, 548),
              size=wx.Size(67, 23), style=0)
        self.button7.Bind(wx.EVT_BUTTON, self.OnCrearSenia,
              id=wxID_DIALOG1BUTTON7)

        self.button8 = wx.Button(id=wxID_DIALOG1BUTTON8, label='Fact.Senia',
              name='button8', parent=self, pos=wx.Point(80, 548),
              size=wx.Size(67, 23), style=0)
        self.button8.Bind(wx.EVT_BUTTON, self.OnFacturarSenia,
              id=wxID_DIALOG1BUTTON8)

        self.button5 = wx.Button(id=wxID_DIALOG1BUTTON5,
              label='Crear ORDEN DE COMPRA', name='button5', parent=self,
              pos=wx.Point(632, 548), size=wx.Size(160, 23), style=0)
        self.button5.Bind(wx.EVT_BUTTON, self.OnCrearOrdenCompra,
              id=wxID_DIALOG1BUTTON5)

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(8, 160), size=wx.Size(784, 240),
              style=0)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)

        self.staticText18 = wx.StaticText(id=wxID_DIALOG1STATICTEXT18,
              label='Nro.Orden', name='staticText18', parent=self,
              pos=wx.Point(128, 124), size=wx.Size(51, 13), style=0)
        self.staticText18.Show(False)

        self.textCtrl16 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL16,
              name='textCtrl16', parent=self, pos=wx.Point(192, 124),
              size=wx.Size(56, 21), style=0, value='')
        self.textCtrl16.SetEditable(False)
        self.textCtrl16.Show(False)

        self.staticText19 = wx.StaticText(id=wxID_DIALOG1STATICTEXT19,
              label='de Compra', name='staticText19', parent=self,
              pos=wx.Point(128, 137), size=wx.Size(52, 13), style=0)
        self.staticText19.Show(False)

        self.textCtrl17 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL17,
              name='textCtrl17', parent=self, pos=wx.Point(408, 408),
              size=wx.Size(56, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl17.SetEditable(False)

        self.textCtrl18 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL18,
              name='textCtrl18', parent=self, pos=wx.Point(312, 408),
              size=wx.Size(56, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl18.SetEditable(False)

        self.staticText20 = wx.StaticText(id=wxID_DIALOG1STATICTEXT20,
              label='Desc.', name='staticText20', parent=self, pos=wx.Point(376,
              408), size=wx.Size(27, 13), style=0)

        self.staticText21 = wx.StaticText(id=wxID_DIALOG1STATICTEXT21,
              label='Subtotal', name='staticText21', parent=self,
              pos=wx.Point(264, 408), size=wx.Size(40, 13), style=0)

        self.staticText23 = wx.StaticText(id=wxID_DIALOG1STATICTEXT23,
              label='Medios de Pago', name='staticText23', parent=self,
              pos=wx.Point(8, 432), size=wx.Size(75, 13), style=0)

        self.comboBox3 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX3,
              name='comboBox3', parent=self, pos=wx.Point(8, 448),
              size=wx.Size(130, 21), style=0, value='')
        self.comboBox3.SetLabel('')
        self.comboBox3.SetEditable(False)
        self.comboBox3.Bind(wx.EVT_COMBOBOX, self.OnPago1,
              id=wxID_DIALOG1COMBOBOX3)

        self.comboBox4 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX4,
              name='comboBox4', parent=self, pos=wx.Point(144, 448),
              size=wx.Size(160, 21), style=0, value='')
        self.comboBox4.SetLabel('')
        self.comboBox4.SetEditable(False)
        self.comboBox4.Enable(False)

        self.staticText24 = wx.StaticText(id=wxID_DIALOG1STATICTEXT24,
              label='Banco', name='staticText24', parent=self, pos=wx.Point(144,
              432), size=wx.Size(29, 13), style=0)

        self.staticText25 = wx.StaticText(id=wxID_DIALOG1STATICTEXT25,
              label='Cuotas', name='staticText25', parent=self,
              pos=wx.Point(312, 432), size=wx.Size(34, 13), style=0)

        self.textCtrl19 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL19,
              name='textCtrl19', parent=self, pos=wx.Point(312, 448),
              size=wx.Size(32, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl19.SetEditable(True)
        self.textCtrl19.Enable(False)
        self.textCtrl19.SetMaxLength(2)

        self.staticText26 = wx.StaticText(id=wxID_DIALOG1STATICTEXT26,
              label='Importe', name='staticText26', parent=self,
              pos=wx.Point(352, 432), size=wx.Size(38, 13), style=0)

        self.textCtrl20 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL20,
              name='textCtrl20', parent=self, pos=wx.Point(352, 448),
              size=wx.Size(72, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl20.SetEditable(True)
        self.textCtrl20.Enable(False)
        self.textCtrl20.SetMaxLength(15)

        self.staticText27 = wx.StaticText(id=wxID_DIALOG1STATICTEXT27,
              label='Lote', name='staticText27', parent=self, pos=wx.Point(432,
              432), size=wx.Size(21, 13), style=0)

        self.textCtrl21 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL21,
              name='textCtrl21', parent=self, pos=wx.Point(424, 448),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl21.SetEditable(True)
        self.textCtrl21.Enable(False)
        self.textCtrl21.SetMaxLength(5)

        self.staticText28 = wx.StaticText(id=wxID_DIALOG1STATICTEXT28,
              label='Cupon', name='staticText28', parent=self, pos=wx.Point(472,
              432), size=wx.Size(31, 13), style=0)

        self.textCtrl22 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL22,
              name='textCtrl22', parent=self, pos=wx.Point(472, 448),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl22.SetEditable(True)
        self.textCtrl22.Enable(False)
        self.textCtrl22.SetMaxLength(5)

        self.staticText29 = wx.StaticText(id=wxID_DIALOG1STATICTEXT29,
              label='Observaciones Pago', name='staticText29', parent=self,
              pos=wx.Point(520, 432), size=wx.Size(98, 13), style=0)

        self.textCtrl23 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL23,
              name='textCtrl23', parent=self, pos=wx.Point(520, 448),
              size=wx.Size(272, 21), style=0, value='')
        self.textCtrl23.SetEditable(True)
        self.textCtrl23.Enable(False)
        self.textCtrl23.SetMaxLength(25)

        self.comboBox5 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX5,
              name='comboBox5', parent=self, pos=wx.Point(8, 472),
              size=wx.Size(130, 21), style=0, value='')
        self.comboBox5.SetLabel('')
        self.comboBox5.SetEditable(False)
        self.comboBox5.Bind(wx.EVT_COMBOBOX, self.OnPago2,
              id=wxID_DIALOG1COMBOBOX5)

        self.comboBox6 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX6,
              name='comboBox6', parent=self, pos=wx.Point(144, 472),
              size=wx.Size(160, 21), style=0, value='')
        self.comboBox6.SetLabel('')
        self.comboBox6.SetEditable(False)
        self.comboBox6.Enable(False)

        self.textCtrl24 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL24,
              name='textCtrl24', parent=self, pos=wx.Point(312, 472),
              size=wx.Size(32, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl24.SetEditable(True)
        self.textCtrl24.Enable(False)
        self.textCtrl24.SetMaxLength(2)

        self.textCtrl25 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL25,
              name='textCtrl25', parent=self, pos=wx.Point(352, 472),
              size=wx.Size(72, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl25.SetEditable(True)
        self.textCtrl25.Enable(False)
        self.textCtrl25.SetMaxLength(15)

        self.textCtrl26 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL26,
              name='textCtrl26', parent=self, pos=wx.Point(424, 472),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl26.SetEditable(True)
        self.textCtrl26.Enable(False)
        self.textCtrl26.SetMaxLength(5)

        self.textCtrl27 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL27,
              name='textCtrl27', parent=self, pos=wx.Point(472, 472),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl27.SetEditable(True)
        self.textCtrl27.Enable(False)
        self.textCtrl27.SetMaxLength(5)

        self.textCtrl28 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL28,
              name='textCtrl28', parent=self, pos=wx.Point(520, 472),
              size=wx.Size(272, 21), style=0, value='')
        self.textCtrl28.SetEditable(True)
        self.textCtrl28.Enable(False)
        self.textCtrl28.SetMaxLength(25)

        self.comboBox7 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX7,
              name='comboBox7', parent=self, pos=wx.Point(8, 496),
              size=wx.Size(130, 21), style=0, value='')
        self.comboBox7.SetLabel('')
        self.comboBox7.SetEditable(False)
        self.comboBox7.Bind(wx.EVT_COMBOBOX, self.OnPago3,
              id=wxID_DIALOG1COMBOBOX7)

        self.comboBox8 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX8,
              name='comboBox8', parent=self, pos=wx.Point(144, 496),
              size=wx.Size(160, 21), style=0, value='')
        self.comboBox8.SetLabel('')
        self.comboBox8.SetEditable(False)
        self.comboBox8.Enable(False)

        self.textCtrl29 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL29,
              name='textCtrl29', parent=self, pos=wx.Point(312, 496),
              size=wx.Size(32, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl29.SetEditable(True)
        self.textCtrl29.Enable(False)
        self.textCtrl29.SetMaxLength(2)

        self.textCtrl30 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL30,
              name='textCtrl30', parent=self, pos=wx.Point(352, 496),
              size=wx.Size(72, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl30.SetEditable(True)
        self.textCtrl30.Enable(False)
        self.textCtrl30.SetMaxLength(15)

        self.textCtrl31 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL31,
              name='textCtrl31', parent=self, pos=wx.Point(424, 496),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl31.SetEditable(True)
        self.textCtrl31.Enable(False)
        self.textCtrl31.SetMaxLength(5)

        self.textCtrl32 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL32,
              name='textCtrl32', parent=self, pos=wx.Point(472, 496),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl32.SetEditable(True)
        self.textCtrl32.Enable(False)
        self.textCtrl32.SetMaxLength(5)

        self.textCtrl33 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL33,
              name='textCtrl33', parent=self, pos=wx.Point(520, 496),
              size=wx.Size(272, 21), style=0, value='')
        self.textCtrl33.SetEditable(True)
        self.textCtrl33.Enable(False)
        self.textCtrl33.SetMaxLength(25)

        self.comboBox9 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX9,
              name='comboBox9', parent=self, pos=wx.Point(8, 520),
              size=wx.Size(130, 21), style=0, value='')
        self.comboBox9.SetLabel('')
        self.comboBox9.SetEditable(False)
        self.comboBox9.Bind(wx.EVT_COMBOBOX, self.OnPago4,
              id=wxID_DIALOG1COMBOBOX9)

        self.comboBox10 = wx.ComboBox(choices=[], id=wxID_DIALOG1COMBOBOX10,
              name='comboBox10', parent=self, pos=wx.Point(144, 520),
              size=wx.Size(160, 21), style=0, value='')
        self.comboBox10.SetLabel('')
        self.comboBox10.SetEditable(False)
        self.comboBox10.Enable(False)

        self.textCtrl34 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL34,
              name='textCtrl34', parent=self, pos=wx.Point(312, 520),
              size=wx.Size(32, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl34.SetEditable(True)
        self.textCtrl34.Enable(False)
        self.textCtrl34.SetMaxLength(2)

        self.textCtrl35 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL35,
              name='textCtrl35', parent=self, pos=wx.Point(352, 520),
              size=wx.Size(72, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl35.SetEditable(True)
        self.textCtrl35.Enable(False)
        self.textCtrl35.SetMaxLength(15)

        self.textCtrl36 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL36,
              name='textCtrl36', parent=self, pos=wx.Point(424, 520),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl36.SetEditable(True)
        self.textCtrl36.Enable(False)
        self.textCtrl36.SetMaxLength(5)

        self.textCtrl37 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL37,
              name='textCtrl37', parent=self, pos=wx.Point(472, 520),
              size=wx.Size(48, 21), style=wx.TE_RIGHT | wx.TE_LEFT, value='')
        self.textCtrl37.SetEditable(True)
        self.textCtrl37.Enable(False)
        self.textCtrl37.SetMaxLength(5)

        self.textCtrl38 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL38,
              name='textCtrl38', parent=self, pos=wx.Point(520, 520),
              size=wx.Size(272, 21), style=0, value='')
        self.textCtrl38.SetEditable(True)
        self.textCtrl38.Enable(False)
        self.textCtrl38.SetMaxLength(25)

        self.staticText30 = wx.StaticText(id=wxID_DIALOG1STATICTEXT30,
              label='TOTAL', name='staticText30', parent=self, pos=wx.Point(312,
              544), size=wx.Size(32, 13), style=0)

        self.textCtrl39 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL39,
              name='textCtrl39', parent=self, pos=wx.Point(352, 544),
              size=wx.Size(72, 21), style=wx.TE_RIGHT | wx.TE_LEFT,
              value='0.00')
        self.textCtrl39.SetEditable(False)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.padre = parent
        fecha = utiles.obtenerFecha("d-m-Y")
        self.textCtrl8.SetLabel(fecha)
        self.cargarComboSucursales()
        self.cargarComboVendedora()
        self.cargarComboMediosPagos()
        self.cargarComboBancos()
        self.crearGrilla()
        self.textCtrl15.SetFocus()
        self.nroOrdenCompra = ""
        self.verificarInterrupcion()
        if self.padre.loginRol != "root": 
            print "Usuario no autorizado"
            self.button1.Enable(False)
            self.button2.Enable(False)
            self.button3.Enable(False)
            self.button4.Enable(False)
            self.button5.Enable(False)
            self.button7.Enable(False)
            self.button8.Enable(False)

    def almacenamientoTemporal(self,fil,borrar=False):
        f=open("OrdenCompra.txt","a")
        idcliente = self.textCtrl1.GetLabel()
        sucursal = str(self.comboBox1.GetSelection())
        vendedora = str(self.comboBox2.GetSelection())
        observacion = self.textCtrl10.GetLabel()
        nroorden = self.textCtrl16.GetLabel()
        #idcliente,sucursal,vendedora,observaciones,ptc,codsussan,descripcion
        #1         2        3         4             5   6         7
        #talle,color,cant,unitario,subtotal,descuento,total
        #8     9     10   11       12       13        14
        encabezado = idcliente+"[;]"+sucursal+"[;]"+vendedora+"[;]"+observacion+"[;]"+nroorden+"[;]"
        if not borrar:
            cadena = str(fil)+"[;]"+encabezado
            for item in xrange(self.grid1.GetNumberCols()):
                cadena+= str(self.grid1.GetCellValue(fil,item))+"[;]"
        else:
            cadena = "-1[;]"+str(fil)+"[;]"+encabezado
        cadena = cadena[:-3]+"\n"
        f.write(cadena)
        f.close()

    def verificarInterrupcion(self):
        if not os.path.isfile("OrdenCompra.txt"):
            f=open("OrdenCompra.txt","w")
            f.close()
            print "Almacenamiento de Ventas iniciado"
            return
        #existe archivo, por lo tanto un corte interrumpio la carga y se debe recuperar la informacion
        f=open("OrdenCompra.txt","r")
        lista = f.readlines()
        f.close()
        if len(lista)==0:
            f=open("OrdenCompra.txt","w")
            f.close()
            print "Almacenamiento de Ventas iniciado"
            return
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE,None)
        print "Restaurando ultima Venta"
        for item in lista:
            cadena = item.split("[;]")
            if cadena[0]=="-1":
                self.grid1.DeleteRows(int(cadena[1]))
                continue
            try:
                fil = int(cadena[0])
            except:
                continue
            #idcliente,sucursal,vendedora,observaciones,ptc,codsussan,descripcion
            #1         2        3         4             5   6         7
            #talle,color,cant,unitario,subtotal,descuento,total
            #8     9     10   11       12       13        14
            self.textCtrl1.SetLabel(cadena[1]) #idcliente
            self.comboBox1.SetSelection(int(cadena[2])) #combo sucursal
            self.cargarComboVendedora()
            self.comboBox2.SetSelection(int(cadena[3])) #combo vendedora
            self.textCtrl10.SetLabel(cadena[4]) #observacion
            self.textCtrl16.SetLabel(cadena[5]) #nroorden
            self.nroOrdenCompra = cadena[5]

            #PTC,codsussan,descripcion,talle,color,cant,$,subtotal,desc,totalitem
            #0   1         2           3     4     5    6 7        8    9
            self.grid1.SetCellValue(fil,0,cadena[6]) #ptc
            self.grid1.SetCellValue(fil,1,cadena[7]) #codsussan
            self.grid1.SetCellValue(fil,2,cadena[8]) #descripcion
            self.grid1.SetCellValue(fil,3,cadena[9]) #talle
            self.grid1.SetCellValue(fil,4,cadena[10]) #color
            self.grid1.SetCellValue(fil,5,cadena[11]) #cantidad
            self.grid1.SetCellValue(fil,6,cadena[12]) #$
            self.grid1.SetCellValue(fil,7,cadena[13]) #subtotal
            self.grid1.SetCellValue(fil,8,cadena[14]) #desc
            self.grid1.SetCellValue(fil,9,cadena[15]) #totalitem

            self.grid1.SetReadOnly(fil,1) #codsussan
            self.grid1.SetReadOnly(fil,2) #descripcion
            self.grid1.SetReadOnly(fil,3) #talle
            self.grid1.SetReadOnly(fil,4) #color
            self.grid1.SetReadOnly(fil,6) #$
            self.grid1.SetReadOnly(fil,7) #subtotal
            self.grid1.SetReadOnly(fil,9) #totalitem
            
            self.grid1.SetCellAlignment(fil, 5, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 6, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 7, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 8, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 9, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            if fil<(self.grid1.GetNumberRows()-1):
                continue
            self.grid1.AppendRows()

        idcliente = self.textCtrl1.GetLabel()
        self.buscarCliente(idcliente=idcliente)
        self.ajustarAnchoColumnas()
        self.actualizaTotales()
        self.grid1.SetFocus()

    def OnSalir(self, event):
        if self.grid1.GetNumberRows()==1: self.OnLimpiar(None, False)
        self.Close()

    def cargarComboSucursales(self):
        sucursal = utiles.getSucursalActiva()
        SQL = "SELECT nombre FROM sucursales WHERE 1 ORDER BY idsucursal"
        resultado = consultasBD.ordenCruda(SQL)
        for item in resultado:
            self.comboBox1.AppendItems(item)
        self.comboBox1.SetSelection(int(sucursal)-1)
        self.comboBox1.SetEditable(False)
        
    def OnCambiaComboSucursal(self, event):
        self.cargarComboVendedora()

    def cargarComboMediosPagos(self):
        def cargarDatos(combo,datos):
            combo.Clear()
            combo.Append("")
            combo.Append("EFECTIVO")
            combo.Append("CTA.CTE.")
            combo.Append("CHEQUE")
            combo.Append("DEPOSITO")
            combo.Append("TRANSFERENCIA")
            combo.Append("REGALO")
            for item in datos:
                if item["nombretarjeta"]=="": continue
                combo.Append(item["nombretarjeta"])
            combo.SetSelection(-1)
            
        r1 = "SELECT DISTINCT nombretarjeta FROM valores WHERE tipoVenta='1' "
        r2 = "ORDER BY nombretarjeta "
        sql = r1+r2
        datos = consultasBD.ordenCrudaDict(sql)
        cargarDatos(self.comboBox3,datos)
        cargarDatos(self.comboBox5,datos)
        cargarDatos(self.comboBox7,datos)
        cargarDatos(self.comboBox9,datos)

    def cargarComboBancos(self):
        def cargarDatos(combo,datos):
            for item in datos:
                aux = item["nombre"]+" / "+str(item["codbanco"])
                combo.Append(aux)
            combo.SetSelection(-1)

        sql = "SELECT nombre,codbanco FROM chq_bancos WHERE 1"
        datos = consultasBD.ordenCrudaDict(sql)
        cargarDatos(self.comboBox4,datos)
        cargarDatos(self.comboBox6,datos)
        cargarDatos(self.comboBox8,datos)
        cargarDatos(self.comboBox10,datos)

    def cargarComboVendedora(self):
        sucursal = str(self.comboBox1.GetSelection()+1)
        r1 = "SELECT nombre FROM vendedoras WHERE sucursal='"+sucursal+"' "
        r2 = "ORDER BY nombre "
        SQL = r1+r2
        resultado = consultasBD.ordenCruda(SQL)
        self.comboBox2.Clear()
        for item in resultado:
            self.comboBox2.AppendItems(item)
        self.comboBox2.SetSelection(-1)
        self.comboBox2.SetEditable(False)

    def ajustarAnchoColumnas(self):
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, None)
        for y in xrange(self.grid1.GetNumberCols()):
            self.grid1.AutoSizeColumn(y,True)
        for fil in xrange(self.grid1.GetNumberRows()):
            self.grid1.SetReadOnly(fil,1) #codsussan
            self.grid1.SetReadOnly(fil,2) #descripcion
            self.grid1.SetReadOnly(fil,3) #talle
            self.grid1.SetReadOnly(fil,4) #color
            self.grid1.SetReadOnly(fil,6) #$
            self.grid1.SetReadOnly(fil,7) #subtotal
            self.grid1.SetReadOnly(fil,9) #totalitem

            self.grid1.SetCellAlignment(fil, 5, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 6, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 7, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 8, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 9, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)

    def crearGrilla(self):
        #PTC,codsussan,descripcion,talle,color,cant,$,subtotal,desc,totalitem
        #0   1         2           3     4     5    6 7        8    9
        columnas=['PTC','CodSussan','Descripcion','Talle','Color','Cant','$','SubTotal','Desc.%','Total\nitem']
        self.grid1.CreateGrid(1,len(columnas))
        for i, col in enumerate(columnas): 
            self.grid1.SetColLabelValue(i, col)

        self.grid1.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        self.grid1.Refresh()

        self.ajustarAnchoColumnas()

    def OnKillCliente1(self, event):
        idcliente = self.textCtrl1.GetLabel()
        self.buscarCliente(idcliente=idcliente)
        
    def OnKillCliente2(self, event):
        nombre = self.textCtrl2.GetLabel()
        self.buscarCliente(nombre=nombre)

    def buscarCliente(self,idcliente="",nombre="",tarjetasussan="",direccion="",telefono="",nrodoc=""):
        if idcliente=="" and nombre=="" and tarjetasussan=="" and \
           direccion=="" and telefono=="" and nrodoc=="":
            return
        if len(idcliente)>0:
            consultaSQL = "SELECT * FROM cliente WHERE idcliente='"+idcliente+"'"
            resultado = consultasBD.ordenCrudaDict(consultaSQL)
            if resultado == () or resultado == None:
                utiles.mensaje(None,"Numero de Cliente no encontrado")
                self.textCtrl1.SetLabel("")
                self.textCtrl2.SetLabel("")
                self.textCtrl1.SetFocus()
                return
            self.textCtrl1.Bind(wx.EVT_KILL_FOCUS, None)
            self.textCtrl2.Bind(wx.EVT_KILL_FOCUS, None)
            self.cargaDatosCliente(resultado[0])
            self.textCtrl1.SetEditable(False)
            self.textCtrl2.SetEditable(False)
            self.grid1.SetFocus()
            return

        if nombre != "*":
            r1 = "SELECT * FROM cliente "
            r2 = "WHERE nombre LIKE '%"+nombre+"%' ORDER BY nombre"
            consultaSQL = r1 + r2
            resultado = consultasBD.ordenCrudaDict(consultaSQL)
            if resultado == () or resultado == None or len(resultado)==0:
                utiles.mensaje(None,"No se encontraron clientes con los datos ingresados")
                self.textCtrl1.SetLabel("")
                self.textCtrl2.SetLabel("")
                self.textCtrl1.SetFocus()
                return
        else:
            r1 = "SELECT * FROM cliente "
            r2 = "WHERE 1 ORDER BY nombre"
            consultaSQL = r1 + r2
            resultado = consultasBD.ordenCrudaDict(consultaSQL)

        if len(resultado)>1:
            import BuscarClientes
            dlg = BuscarClientes.Dialog1(self,resultado)
            try:
                dlg.ShowModal()
            finally:
                if dlg.seleccion == "":
                    self.textCtrl1.SetLabel("")
                    self.textCtrl2.SetLabel("")
                    self.textCtrl1.SetFocus()
                    return
                clientes = dlg.seleccion
                dlg.Destroy()
        else:
            clientes = resultado[0]
        self.textCtrl1.Bind(wx.EVT_KILL_FOCUS, None)
        self.textCtrl2.Bind(wx.EVT_KILL_FOCUS, None)
        self.cargaDatosCliente(clientes)
        self.textCtrl1.SetEditable(False)
        self.textCtrl2.SetEditable(False)
        self.grid1.SetFocus()
        return

    def cargaDatosCliente(self,datos):
        idcliente = datos["idcliente"]
        nombre = datos["nombre"]
        direccion = datos["direccion"]
        provincia = datos["provincia"]
        cp = datos["cp"]
        direccion = direccion+"/"+provincia+"/"+cp
        telefono = datos["telefono"]
        situacionfiscal = datos["situacionfiscal"]
        email = datos["email"]
        cumple = datos["cumple"]
        tipodoc = datos["tipodoc"]
        numerodoc = datos["nrodoc"]
        tarjetasussan = datos["tarjetasussan"]
        documento = tipodoc+"/"+numerodoc

        self.textCtrl1.SetLabel(idcliente)
        self.textCtrl2.SetLabel(nombre)
        self.textCtrl3.SetLabel(direccion)
        self.textCtrl4.SetLabel(telefono)
        self.textCtrl5.SetLabel(situacionfiscal)
        self.textCtrl6.SetLabel(email)
        self.textCtrl7.SetLabel(cumple)
        self.textCtrl9.SetLabel(documento)
        self.textCtrl14.SetLabel(tarjetasussan)
        
        self.almacenamientoTemporal(0)

    def OnLimpiar(self, event, advertencia=True):
        if advertencia:
            import Dialog2
            texto = "ATENCION: \n\n"
            texto+= "Se perderan los datos cargados en pantalla.\n"
            texto+= "Desea proceder?\n\n\n"
            texto+= "ACEPTAR = si                    CANCELAR = no"
            confirma = Dialog2.Dialog2(self, texto, default="no")
            try:
                confirma.ShowModal()
                if not confirma.getSeleccion():
                    return
            finally:
                pass
        try:
            os.remove("OrdenCompra.txt")
        except:
            print "Archivo temporal inexistente"
        self.textCtrl1.SetLabel("")
        self.textCtrl2.SetLabel("")
        self.textCtrl3.SetLabel("")
        self.textCtrl4.SetLabel("")
        self.textCtrl5.SetLabel("")
        self.textCtrl6.SetLabel("")
        self.textCtrl7.SetLabel("")
        self.textCtrl8.SetLabel(utiles.obtenerFecha("d-m-Y"))
        self.textCtrl9.SetLabel("")
        self.textCtrl10.SetLabel("")
        self.textCtrl16.SetLabel("")
        #self.textCtrl1.SetEditable(True)
        #self.textCtrl2.SetEditable(True)
        self.textCtrl15.SetEditable(True)
        #self.textCtrl1.Bind(wx.EVT_KILL_FOCUS, self.OnKillCliente1)
        #self.textCtrl2.Bind(wx.EVT_KILL_FOCUS, self.OnKillCliente2)
        self.textCtrl15.Bind(wx.EVT_KILL_FOCUS, self.OnKillBuscarCliente)
        self.grid1.ClearGrid()
        for item in xrange(self.grid1.GetNumberRows()):
            self.grid1.DeleteRows()
        self.grid1.AppendRows()
        self.ajustarAnchoColumnas()
        sucursal = utiles.getSucursalActiva()
        self.comboBox1.SetSelection(int(sucursal)-1)
        self.comboBox2.SetSelection(-1)
        self.button7.Enable(True)
        self.button8.Enable(True)
        self.textCtrl15.SetFocus()
        self.nroOrdenCompra = ""

    def OnModifCliente(self, event):
        import ModificaClientes
        idcliente = str(self.textCtrl1.GetLabel())
        if idcliente != "":
            ventana = ModificaClientes.ModifClientes(self,idcliente)
            try:
                ventana.ShowModal()
            finally:
                ventana.Destroy()
        else:
             utiles.mensaje(self,"No hay un cod. de cliente cargado")
             self.textCtrl1.SetFocus()
             return
        self.OnKillCliente1(None)

    def OnNuevoCliente(self, event):
        import ABMclientes
        ventana = ABMclientes.ABMclientes(self)
        try:
            ventana.ShowModal()
            idcliente = ventana.Muestraid()
            if idcliente != "":
                #busca el cliente nuevo con el nuevo ID
                self.textCtrl1.SetLabel(idcliente)
                self.OnKillCliente1(None)
        finally:
            ventana.Destroy()
        self.textCtrl1.SetFocus()

    def buscarVendedora(self):
        sucursal = str(self.comboBox1.GetSelection()+1)
        nombre = self.comboBox2.GetLabel()
        r1 = "SELECT idvendedora FROM vendedoras "
        r2 = "WHERE sucursal='"+sucursal+"' AND nombre='"+nombre+"' "
        sql = r1+r2
        datos = consultasBD.ordenCrudaDict(sql)
        if len(datos)!=1 or datos==() or datos==None:
            return ""
        return str(datos[0]["idvendedora"])

    def OnCrearSenia(self, event):
        import VentasSenias
        incodigo = self.textCtrl1.GetLabel()
        inapellido = self.textCtrl2.GetLabel()
        if incodigo == "" and inapellido=="":
            utiles.mensaje(None,"Debe seleccionar un cliente")
            self.textCtrl1.SetFocus()
            return
        if incodigo == "1-2":
            utiles.mensaje(None,"El cliente 1-2 no puede seniar")
            self.textCtrl1.SetFocus()
            return
        vendedora = self.comboBox2.GetLabel()
        idvendedora = self.buscarVendedora()
        if vendedora == "":
            utiles.mensaje(None,"Debe especificar nombre vendedora")
            self.comboBox2.SetFocus()
            return
        dlg = VentasSenias.Dialog1(self, incodigo, inapellido, vendedora, idvendedora)
        try:
            dlg.ShowModal()
        finally:
            dlg.Destroy() 

    def OnFacturarSenia(self, event):
        import VentasSeniasFacturacion
        incodigo = self.textCtrl1.GetLabel()
        inapellido = self.textCtrl2.GetLabel()
        vendedora = self.comboBox2.GetLabel()
        idvendedora = self.buscarVendedora()
        if incodigo == "" and inapellido=="":
            utiles.mensaje(None,"Debe seleccionar un cliente")
            self.textCtrl1.SetFocus()
            return
        if incodigo == "1-2":
            utiles.mensaje(None,"El cliente 1-2 no puede seniar")
            self.grid1.SetFocus()
            return
        if vendedora == "Vendedoras...":
            utiles.mensaje(None,"Debe especificar nombre vendedora")
            self.comboBox2.SetFocus()
            return
        dlg = VentasSeniasFacturacion.Dialog1(self, incodigo, inapellido, vendedora, idvendedora)
        try:
            dlg.ShowModal()
            listaPTC = dlg.getDatosSenias()
        finally:
            dlg.Destroy() 
        if listaPTC==[]:
            return

        self.grid1.ClearGrid()
        self.grid1.SetGridCursor(0,0)
        #self.grid1.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        #self.grid1.EnableEditing(False)
        self.button7.Enable(False)
        self.button8.Enable(False)

        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, None)
        for fil,datos in enumerate(listaPTC):
            ptc = datos["ptc"]
            codsussan = datos["codsussan"]
            descripcion = datos["descripcion"]
            talle = datos["talle"]
            color = datos["color"]
            cantidad = datos["cantidad"]
            preciounitario = datos["preciounitario"]
            subtotal = datos["subtotal"]

            self.grid1.SetCellValue(fil,0,ptc)
            self.grid1.SetCellValue(fil,1,codsussan)
            self.grid1.SetCellValue(fil,2,descripcion)
            self.grid1.SetCellValue(fil,3,talle)
            self.grid1.SetCellValue(fil,4,color)
            self.grid1.SetCellValue(fil,5,cantidad)
            self.grid1.SetCellValue(fil,6,preciounitario)
            self.grid1.SetCellValue(fil,7,subtotal)
            self.grid1.SetCellValue(fil,8,"0.00")
            self.grid1.SetCellValue(fil,9,subtotal)

            self.grid1.SetCellAlignment(fil, 5, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 6, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 7, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 8, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            self.grid1.SetCellAlignment(fil, 9, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
            
            self.grid1.AppendRows()

        self.grid1.ForceRefresh()
        self.ajustarAnchoColumnas()
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)

    def buscarDatosProducto(self,ptc):
        r1 = "SELECT p.codsussan,p.descri,p.preciolt1,ptc.talle,ptc.color, \n"
        r2 = "       c.nombre \n "
        r3 = "FROM producto_talle_color ptc, productos p, colores c "
        r4 = "WHERE ptc.id='"+ptc+"' AND ptc.codsussan=p.codsussan \n"
        r5 = "      AND ptc.color=c.idcolor "
        sql = r1+r2+r3+r4+r5
        resultado = consultasBD.ordenCrudaDict(sql)
        if resultado == () or resultado == None or len(resultado)==0:
            resultado = None
        else:
            resultado = resultado[0]
        return resultado

    def borrarRenglon(self,fil):
        for col in xrange(self.grid1.GetNumberCols()):
            self.grid1.SetCellValue(fil,col,"")
        self.almacenamientoTemporal(fil,True)

    def subtotalRenglon(self,fil):
        #self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, None)
        cantidad = self.grid1.GetCellValue(fil,5)
        cantidad = Decimal(cantidad).quantize(Decimal("0.10"))
        unitario = self.grid1.GetCellValue(fil,6)
        unitario = Decimal(unitario).quantize(Decimal("0.10"))
        subtotal = cantidad*unitario
        subtotal = Decimal(subtotal).quantize(Decimal("0.10"))
        self.grid1.SetCellValue(fil,7,str(subtotal))
        descuento = self.grid1.GetCellValue(fil,8)
        try:
            descuento = Decimal(descuento).quantize(Decimal("0.10"))
        except:
            descuento = Decimal("0.00").quantize(Decimal("0.10"))
        self.grid1.SetCellValue(fil,9,str(descuento))
        cien = Decimal("100.00")
        montodescuento = subtotal*descuento/cien
        montodescuento = Decimal(montodescuento).quantize(Decimal("0.10"))
        totalrenglon = subtotal - montodescuento
        self.grid1.SetCellValue(fil,9,str(totalrenglon))

        #self.almacenamientoTemporal(fil)
        self.ajustarAnchoColumnas()
        self.grid1.Refresh()

    def publicarRenglon(self,fil,datos):
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, None)

        #PTC,codsussan,descripcion,talle,color,cant,$,subtotal,desc,totalitem
        #0   1         2           3     4     5    6 7        8    9
        self.grid1.SetCellValue(fil,1,datos["codsussan"]) #codsussan
        self.grid1.SetCellValue(fil,2,datos["descri"]) #descripcion
        self.grid1.SetCellValue(fil,3,datos["talle"]) #talle
        self.grid1.SetCellValue(fil,4,datos["nombre"]+" / "+str(datos["color"])) #color
        self.grid1.SetCellValue(fil,5,"1") #cantidad
        preciounitario = Decimal(str(datos["preciolt1"])).quantize(Decimal("0.10"))
        preciounitario = str(preciounitario)
        self.grid1.SetCellValue(fil,6,preciounitario) #$
        self.grid1.SetCellValue(fil,8,"0.00") #descuento
        
        self.subtotalRenglon(fil)

        self.grid1.SetReadOnly(fil,1) #codsussan
        self.grid1.SetReadOnly(fil,2) #descripcion
        self.grid1.SetReadOnly(fil,3) #talle
        self.grid1.SetReadOnly(fil,4) #color
        self.grid1.SetReadOnly(fil,6) #$
        self.grid1.SetReadOnly(fil,7) #subtotal
        #self.grid1.SetReadOnly(fil,9) #totalitem
        
        self.grid1.SetCellAlignment(fil, 5, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 6, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 7, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 8, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetCellAlignment(fil, 9, wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        
        if (self.grid1.GetNumberRows()-1)==fil:
            self.grid1.AppendRows()

        #self.almacenamientoTemporal(fil)
        self.ajustarAnchoColumnas()

    def ptcrepetido(self,ptc):
        for fil in xrange(self.grid1.GetNumberRows()):
            if self.grid1.GetCellValue(fil,0)==ptc:
                #PTC,codsussan,descripcion,talle,color,cant,$,subtotal,desc,totalitem
                #0   1         2           3     4     5    6 7        8    9
                cantidad = int(self.grid1.GetCellValue(fil,5))+1
                preciounitario = Decimal(self.grid1.GetCellValue(fil,6))
                preciounitario = preciounitario.quantize(Decimal("0.10"))
                subtotal = Decimal(str((cantidad*preciounitario))).quantize(Decimal("0.10"))
                descuento = Decimal(self.grid1.GetCellValue(fil,8)).quantize(Decimal("0.10"))
                aux = subtotal*descuento/100
                totalitem = subtotal-aux
                totalitem = Decimal(str(totalitem)).quantize(Decimal("0.10"))
                self.grid1.SetCellValue(fil,5,str(cantidad))
                self.grid1.SetCellValue(fil,7,str(subtotal))
                self.grid1.SetCellValue(fil,8,str(descuento))
                self.grid1.SetCellValue(fil,9,str(totalitem))
                self.actualizaTotales()
                self.almacenamientoTemporal(fil)
                return True
        return False

    def actualizaTotales(self):
        #self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, None)
        #self.almacenamientoTemporal(0)
        subtotal1 = Decimal("0.00")
        descuentos = Decimal("0.00")
        subtotal2 = Decimal("0.00")
        #PTC,codsussan,descripcion,talle,color,cant,$,subtotal,desc,totalitem
        #0   1         2           3     4     5    6 7        8    9
        for fil in xrange(self.grid1.GetNumberRows()):
            if self.grid1.GetCellValue(fil,0)=="": continue
            subtotal = Decimal(self.grid1.GetCellValue(fil,7)).quantize(Decimal("0.10"))
            totalitem = Decimal(self.grid1.GetCellValue(fil,9)).quantize(Decimal("0.10"))
            descuento = subtotal-totalitem
            subtotal1+= subtotal
            descuentos+= descuento
            subtotal2+= totalitem
        iva = Decimal(str(subtotal2/Decimal("1.21"))).quantize(Decimal("0.10"))
        iva = subtotal2-iva

        self.textCtrl18.SetLabel(str(subtotal1))
        self.textCtrl17.SetLabel(str(descuentos))
        self.textCtrl11.SetLabel(str(subtotal1-descuentos-iva))
        self.textCtrl12.SetLabel(str(iva))
        self.textCtrl13.SetLabel(str(subtotal2))

    def OnGrid1Cambia(self, event):
        fil = self.grid1.GetGridCursorRow()
        col = self.grid1.GetGridCursorCol()
        celda = self.grid1.GetCellValue(fil,col)

        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, None)
        #PTC,codsussan,descripcion,talle,color,cant,$,subtotal,desc,totalitem
        #0   1         2           3     4     5    6 7        8    9
        #col: 1 - PTC   5 - cantidad   8 - descuento 
        if col == 0: #PTC
            if celda == "": #blanco en primer casillero: borra renglon
                self.borrarRenglon(fil)
                self.actualizaTotales()
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            ptc = celda
            self.grid1.SetCellValue(fil,0,"")
            if self.ptcrepetido(ptc):
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            self.grid1.SetCellValue(fil,0,ptc)
            datos = self.buscarDatosProducto(ptc)
            if datos == None:
                utiles.mensaje(None,"No existe el PTC")
                self.grid1.SetCellValue(fil,0,"")
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            self.publicarRenglon(fil,datos)
            self.actualizaTotales()
            self.almacenamientoTemporal(fil)
            self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
            return
        if col == 5: #cantidad
            if self.grid1.GetCellValue(fil,0) == "":
                self.grid1.SetCellValue(fil,col,"")
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            try:
                valor = Decimal(celda)
            except:
                utiles.mensaje(None,"Valor incorrecto en columna CANTIDAD")
                self.grid1.SetCellValue(fil,col,"1")
                self.subtotalRenglon(fil)
                self.actualizaTotales()
                self.almacenamientoTemporal(fil)
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            if valor == Decimal("0.00"):
                utiles.mensaje(None,"Cantidad debe ser diferente a 0 (cero)")
                self.grid1.SetCellValue(fil,col,"1")
                self.subtotalRenglon(fil)
                self.actualizaTotales()
                self.almacenamientoTemporal(fil)
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            self.subtotalRenglon(fil)
            self.actualizaTotales()
            self.almacenamientoTemporal(fil)
            self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
            return
        if col == 8: #modifica descuento
            if self.grid1.GetCellValue(fil,0) == "":
                self.grid1.SetCellValue(fil,col,"")
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            try:
                valor = Decimal(celda)
            except:
                utiles.mensaje(None,"Valor incorrecto en columna DESCUENTO")
                self.grid1.SetCellValue(fil,col,"0.00")
                self.subtotalRenglon(fil)
                self.actualizaTotales()
                self.almacenamientoTemporal(fil)
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            if valor < Decimal("0.00"):
                utiles.mensaje(None,"El descuento debe ser 0 (cero) o valor mayor")
                self.grid1.SetCellValue(fil,col,"0.00")
                self.subtotalRenglon(fil)
                self.actualizaTotales()
                self.almacenamientoTemporal(fil)
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            self.subtotalRenglon(fil)
            self.actualizaTotales()
            self.almacenamientoTemporal(fil)
            self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
            return
        if col == 9: #total renglon -> actualizar descuento
            if self.grid1.GetCellValue(fil,0) == "":
                self.grid1.SetCellValue(fil,col,"")
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            try:
                valor = Decimal(celda)
            except:
                utiles.mensaje(None,"Valor incorrecto en columna TOTAL")
                self.subtotalRenglon(fil)
                self.actualizaTotales()
                self.almacenamientoTemporal(fil)
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            if valor < Decimal("0.00"):
                utiles.mensaje(None,"Valor incorrecto en columna TOTAL")
                self.subtotalRenglon(fil)
                self.actualizaTotales()
                self.almacenamientoTemporal(fil)
                self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)
                return
            subtotal = self.grid1.GetCellValue(fil,7)
            descuento = subtotal-valor
            

        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnGrid1Cambia)

    def OnKillObservacion(self, event):
        self.almacenamientoTemporal(0)

    def OnCheckBox(self, event):
        self.almacenamientoTemporal(0)

    def OnCambiaVendedora(self, event):
        self.almacenamientoTemporal(0)

    def OnKillBuscarCliente(self, event):
        parametro = self.textCtrl15.GetLabel()
        if parametro == "": return
        self.OnBuscarCliente(None)
        
    def OnBuscarCliente(self, event):
        parametro = self.textCtrl15.GetLabel()
        if parametro == "": return

        sql = "SELECT idcliente FROM cliente WHERE idcliente LIKE '%"+parametro+"%' "
        resultado1 = consultasBD.ordenCruda(sql)
        sql = "SELECT idcliente FROM cliente WHERE nombre LIKE '%"+parametro+"%' "
        resultado2 = consultasBD.ordenCruda(sql)
        sql = "SELECT idcliente FROM cliente WHERE tarjetasussan LIKE '%"+parametro+"%' "
        resultado3 = consultasBD.ordenCruda(sql)
        sql = "SELECT idcliente FROM cliente WHERE direccion LIKE '%"+parametro+"%' "
        resultado4 = consultasBD.ordenCruda(sql)
        sql = "SELECT idcliente FROM cliente WHERE telefono LIKE '%"+parametro+"%' "
        resultado5 = consultasBD.ordenCruda(sql)
        sql = "SELECT idcliente FROM cliente WHERE email LIKE '%"+parametro+"%' "
        resultado6 = consultasBD.ordenCruda(sql)
        sql = "SELECT idcliente FROM cliente WHERE nrodoc LIKE '%"+parametro+"%' "
        resultado7 = consultasBD.ordenCruda(sql)
        
        idcliente = []
        for item in resultado1: idcliente.append(item[0])
        for item in resultado2: idcliente.append(item[0])
        for item in resultado3: idcliente.append(item[0])
        for item in resultado4: idcliente.append(item[0])
        for item in resultado5: idcliente.append(item[0])
        for item in resultado6: idcliente.append(item[0])
        for item in resultado7: idcliente.append(item[0])

        aux = set(idcliente)
        idcliente = list(aux)

        cadena = " idcliente IN ("
        for item in idcliente:
            cadena+= "'"+item+"',"
        cadena = cadena[:-1]+")"

        r1 = "SELECT * FROM cliente "
        r2 = "WHERE "+cadena+" ORDER BY nombre"
        consultaSQL=r1+r2
        resultado=consultasBD.ordenCrudaDict(consultaSQL)

        if len(resultado)>1:
            import BuscarClientes
            dlg = BuscarClientes.Dialog1(self,resultado)
            try:
                dlg.ShowModal()
            finally:
                if dlg.seleccion == "":
                    self.textCtrl1.SetLabel("")
                    self.textCtrl2.SetLabel("")
                    self.textCtrl15.SetLabel("")
                    self.textCtrl15.SetFocus()
                    return
                clientes = dlg.seleccion
                dlg.Destroy()
        else:
            clientes = resultado[0]
        self.textCtrl1.Bind(wx.EVT_KILL_FOCUS, None)
        self.textCtrl2.Bind(wx.EVT_KILL_FOCUS, None)
        self.textCtrl15.Bind(wx.EVT_KILL_FOCUS, None)
        self.textCtrl15.SetLabel("")
        self.textCtrl15.SetEditable(True)
        self.cargaDatosCliente(clientes)
        self.comboBox2.SetFocus()
        return

    def verificar(self):
        if self.comboBox2.GetLabel()=="":
            texto = "Debe informar la Vendedora"
            utiles.mensaje(None,texto)
            self.comboBox2.SetFocus()
            return False
        if self.textCtrl1.GetLabel()=="":
            texto = "Debe informar el Cliente"
            utiles.mensaje(None,texto)
            self.textCtrl15.SetFocus()
            return False
        i = 0
        for fil in xrange(self.grid1.GetNumberRows()):
            if self.grid1.GetCellValue(fil,0)=="": continue
            i+= 1
        if i==0:
            texto = "No hay productos en la Orden de Compra"
            utiles.mensaje(None,texto)
            self.grid1.SetFocus()
            return False
        return True

    def OnImprimir(self, event):
        print "Imprimir ORDEN DE COMPRA"
        if not self.verificar(): return
        idcliente = self.textCtrl1.GetLabel()
        if idcliente == "": return
        if self.nroOrdenCompra == "":
            sucursal = utiles.getSucursalActiva()
            consultasBD.begin()
            self.nroOrdenCompra = utiles.secuencia("ordencompra",sucursal)
            consultasBD.commit()
            self.textCtrl16.SetLabel(self.nroOrdenCompra)
            self.almacenamientoTemporal(0)
        tam = "1.5"
        sucursal = self.comboBox1.GetLabel()
        usuario = self.comboBox2.GetLabel()
        nombre = self.textCtrl2.GetLabel()
        fecha = self.textCtrl8.GetLabel()
        cadena = ""
        nroPagina = 1
        totalRenglones = 0
        for fil in xrange(self.grid1.GetNumberRows()):
            if self.grid1.GetCellValue(fil,0) == "": continue
            totalRenglones+= 1
        totalPagina = (totalRenglones / 8) + 1

        def encabezado(cadena):
            cadena+= '<table width="100%" align="center" cellspacing="0" border="0">\n'
            cadena+= '<tr>\n'
            cadena+= '<td align="left" valign="top"><font size="'+tam+'">Sucursal: '+sucursal+'</font></td>\n'
            cadena+= '<td align="right" valign="top"><font size="'+tam+'">Vendedora: '+usuario+'</font></td>\n'
            cadena+= '</tr>\n'
            cadena+= '<tr>\n'
            cadena+= '<td align="left" valign="top"><font size="'+tam+'">'+nombre+'/'+idcliente+'</font></td>\n'
            nroP = str(nroPagina)
            totP = str(totalPagina)
            cadena+= '<td align="right" valign="top"><font size="'+tam+'">'+fecha+' - P&aacute;gina '+nroP+'/'+totP+'</font></td>\n'
            cadena+= '</tr>\n'
            cadena+= '</table>\n'
            cadena+= '<table width="100%" align="center" cellspacing="0" border="1">\n'
            cadena+= '<tr><td align="left" valign="top"><font size="'+tam+'">PTC</font></td>\n'
            cadena+= '    <td align="left" valign="top"><font size="'+tam+'">Descripcion</font></td>\n'
            cadena+= '    <td align="right" valign="top"><font size="'+tam+'">Cant</font></td>\n'
            cadena+= '    <td align="right" valign="top"><font size="'+tam+'">$</font></td>\n'
            cadena+= '    <td align="right" valign="top"><font size="'+tam+'">Subtotal</font></td>\n'
            cadena+= '    <td align="right" valign="top"><font size="'+tam+'">Desc</font></td>\n'
            cadena+= '    <td align="right" valign="top"><font size="'+tam+'">Total</font></td>\n'
            cadena+= '</tr>'
            return cadena
        
        def saltoPagina(cadena):
            cadena+= '</table><!-- salto de pagina -->\n'
            cadena+= '<p STYLE="page-break-after: always;"></p>\n'
            cadena+= '<table width="100%" align="center" cellspacing="0" border="0">'
            cadena+= '<tr><td valign="top" align="left">'
            cadena+= '<font size="2"><strong>SUSSAN S.R.L.</strong></font><br>'
            cadena+= '<font size="1"><strong>indumentaria | calzados | accesorios</strong></font>'
            cadena+= '</td>'
            cadena+= '<td valign="top" align="right">'
            cadena+= '<font size="2"><strong>ORDEN DE COMPRA { % nroorden % } </strong></font><br>'
            cadena+= '<font size="1"><strong>Documento NO valido como factura</strong></font>'
            cadena+= '</td>'
            cadena+= '</tr>'
            cadena+= '</table>'
            
            cadena = encabezado(cadena)
            return cadena

        total = Decimal("0.00")
        renglon = 0
        cadena = encabezado(cadena)
        for fil in xrange(self.grid1.GetNumberRows()):
            if self.grid1.GetCellValue(fil,0)=="": continue
            #PTC,codsussan,descripcion,talle,color,cant,$,subtotal,desc,totalitem
            #0   1         2           3     4     5    6 7        8    9
            cadena+= '<tr>\n'
            ptc = self.grid1.GetCellValue(fil,0)
            codsussan = self.grid1.GetCellValue(fil,1)
            descrip = self.grid1.GetCellValue(fil,2)[:15]
            talle = self.grid1.GetCellValue(fil,3)
            color = self.grid1.GetCellValue(fil,4)
            color,idcolor = color.split(" / ")
            color = color[:6]
            descripcion = codsussan+" / "+talle+" / "+color+"<br>"+descrip
            cant = self.grid1.GetCellValue(fil,5)
            precio = self.grid1.GetCellValue(fil,6)
            subtotal = self.grid1.GetCellValue(fil,7)
            descuento = self.grid1.GetCellValue(fil,8)
            totalitem = self.grid1.GetCellValue(fil,9)
            try:
                aux = Decimal(totalitem).quantize(Decimal("0.10"))
            except:
                aux = Decimal("0.00")
            total+= aux
            
            cadena+= '<td align="left" valign="top"><font size="'+tam+'">'+ptc+'</font></td>\n'
            tam = "1"
            cadena+= '<td align="left" valign="top"><font size="'+tam+'">'+descripcion+'</font></td>\n'
            tam = "1.5"
            cadena+= '<td align="right" valign="top"><font size="'+tam+'">'+cant+'</font></td>\n'
            cadena+= '<td align="right" valign="top"><font size="'+tam+'">'+precio+'</font></td>\n'
            cadena+= '<td align="right" valign="top"><font size="'+tam+'">'+subtotal+'</font></td>\n'
            cadena+= '<td align="right" valign="top"><font size="'+tam+'">'+descuento+'</font></td>\n'
            cadena+= '<td align="right" valign="top"><font size="'+tam+'">'+totalitem+'</font></td>\n'
            cadena+= '</tr>\n'
            renglon+= 1

            if renglon >= 10:
                renglon = 0
                nroPagina+= 1
                cadena = saltoPagina(cadena)

        if renglon >= 10:
            renglon = 0
            nroPagina+= 1
            cadena = saltoPagina(cadena)

        cadena+= '<tr>\n'
        cadena+= '<td align="left" valign="top"><font size="'+tam+'"></font></td>\n'
        cadena+= '<td align="left" valign="top" colspan="5"><font size="'+tam+'">TOTAL</font></td>\n'
        cadena+= '<td align="right" valign="top"><font size="'+tam+'">'+str(total)+'</font></td>\n'
        cadena+= '</tr>\n'

        cadena+= '</table>\n'
        
        f=open("modelos\\ordenCompra.html","r")
        modelo = f.read()
        f.close()
        modelo = modelo.replace('{ % datos % }',cadena)
        modelo = modelo.replace('{ % nroorden % }',self.nroOrdenCompra)
        f=open("c:\\matrisamor.doc\\reporte-ordenCompra.html","w")
        f.write(modelo+"\n")
        f.close()
        
        utiles.mensaje(None,"Orden de Compra impresa")

    def OnCrearOrdenCompra(self, event):
        print "Crear Orden de Compra"
        if not self.verificar(): return
        idcliente = self.textCtrl1.GetLabel()
        if idcliente == "": return
        if self.nroOrdenCompra == "":
            sucursal = utiles.getSucursalActiva()
            consultasBD.begin()
            self.nroOrdenCompra = utiles.secuencia("ordencompra",sucursal)
            self.textCtrl16.SetLabel(self.nroOrdenCompra)
            self.almacenamientoTemporal(0)

        #preparando datos para guardar
        nroorden = self.nroOrdenCompra
        fecha = utiles.obtenerFecha("Y-m-d h:m:s")
        idcliente = self.textCtrl1.GetLabel()
        vendedora = self.buscarVendedora()
        observaciones = self.textCtrl10.GetLabel()
        subtotal1 = self.textCtrl18.GetLabel()
        descuentos = self.textCtrl17.GetLabel()
        subtotal2 = self.textCtrl11.GetLabel()
        iva = self.textCtrl12.GetLabel()
        total = self.textCtrl13.GetLabel()
        
        try:
            consultasBD.begin()
            r1 = "INSERT INTO ordencompra(nroorden,fecha,idcliente,vendedora,\n"
            r2 = "            sucursal,observaciones,subtotal1,descuentos,\n"
            r3 = "            subtotal2,iva,total) \n"
            r4 = "VALUES ('%s','%s''%s''%s'," % (nroorden,fecha,idcliente,vendedora)
            r5 = "'%s','%s','%s','%s'," % (sucursal,observaciones,subtotal1,descuentos)
            r6 = "'%s','%s','%s')" % (subtotal2,iva,total)
            sql = r1+r2+r3+r4+r5+r6
            consultasBD.commit()
        except:
            consultasBD.rollback()
            texto = "Error: no se graba la Orden de Compra\n"
            texto+= "Intentelo mas tarde"
            utiles.mensaje(None,texto)
            return

    def cargaPagos(self,combo1,combo2,text1,text2,text3,text4,text5):
        def habilita(habilitacion=True):
            text1.SetEditable(habilitacion)
            text2.SetEditable(habilitacion)
            text3.SetEditable(habilitacion)
            text4.SetEditable(habilitacion)
            text5.SetEditable(habilitacion)
            text1.Enable(habilitacion)
            text2.Enable(habilitacion)
            text3.Enable(habilitacion)
            text4.Enable(habilitacion)
            text5.Enable(habilitacion)
            if combo1.GetSelection()<7:
                text3.SetEditable(False)
                text4.SetEditable(False)
                text3.Enable(False)
                text4.Enable(False)
        #1 EFECTIVO 2 CTACTE 3 CHEQUE 4 DEPOSITO 5 TRANSFERENCIA 6 REGALO
        if combo1.GetSelection()>2 and combo1.GetSelection()!=6:
            #con datos bancarios obligatoriamente informar
            combo2.Enable(True)
            habilita()
            return
        combo2.Enable(False)
        combo2.SetSelection(-1)
        if combo1.GetSelection()==0: #poner a 0
            text1.SetLabel("")
            text2.SetLabel("0.00")
            text3.SetLabel("")
            text4.SetLabel("")
            text5.SetLabel("")
            habilita(False)
            return
        habilita()
        #informa efectivo, ctacte, regalo
        print "Efectivo, Cta Cte, Regalo"

    def OnPago1(self, event):
        self.cargaPagos(self.comboBox3,self.comboBox4,self.textCtrl19,\
                        self.textCtrl20,self.textCtrl21,self.textCtrl22,\
                        self.textCtrl23)

    def OnPago2(self, event):
        self.cargaPagos(self.comboBox5,self.comboBox6,self.textCtrl24,\
                        self.textCtrl25,self.textCtrl26,self.textCtrl27,\
                        self.textCtrl28)

    def OnPago3(self, event):
        self.cargaPagos(self.comboBox7,self.comboBox8,self.textCtrl29,\
                        self.textCtrl30,self.textCtrl31,self.textCtrl32,\
                        self.textCtrl33)

    def OnPago4(self, event):
        self.cargaPagos(self.comboBox9,self.comboBox10,self.textCtrl34,\
                        self.textCtrl35,self.textCtrl36,self.textCtrl37,\
                        self.textCtrl38)
